/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaDomain;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DomainDao {
	private static Log logger = LogFactory.getLog(DomainDao.class);
	
	public static List getAllDomains()throws BifangException{
		Tx tx = null;
		
		try {
			tx = TxManager.start();
			String sql = "from SoaDomain ";
			List domains = DaoManager.getDao().find(sql);
			tx.commit();
			
			return domains;
			
		} catch (Throwable e) {
			logger.error("DomainDao.getAllDomains()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"getAllDomains() error",e);
		}finally{
			tx.end();
		}
		
		
	}
	
	public static SoaDomain getLocalDomain()throws BifangException{
		Tx tx = null;
		
		try {
			tx = TxManager.start();
			String sql = "from SoaDomain s where s.local = ?";
			List domains = DaoManager.getDao().find(sql,new Boolean(true));
			tx.commit();
			
			if(domains.size() > 0)
				return (SoaDomain)domains.get(0);
			else
				return null;
			
		} catch (Throwable e) {
			logger.error("DomainDao.getAllDomains()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"getAllDomains() error",e);
		}finally{
			tx.end();
		}
		
		
	}
	
	public static void createDomain(SoaDomain d)throws BifangException{
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().save(d);
			DaoManager.getDao().flush();
			tx.commit();			
		} catch (Throwable e) {
			logger.error("DomainDao.createDomain()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"createDomain() error",e);
		}finally{
			tx.end();
		}
		
	}
	
	public static void modifyDomain(SoaDomain d)throws BifangException{
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().merge(d);
			DaoManager.getDao().flush();
			tx.commit();			
		} catch (Throwable e) {
			logger.error("DomainDao.modifyDomain()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"modifyDomain() error",e);
		}finally{
			tx.end();
		}
		
	}
	
	public static void removeDomain(SoaDomain d)throws BifangException{
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().delete(d);
			DaoManager.getDao().flush();
			tx.commit();			
		} catch (Throwable e) {
			logger.error("DomainDao.removeDomain()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"removeDomain() error",e);
		}finally{
			tx.end();
		}
		
	}
	
	public static void removeDomain(String id)throws BifangException{
		Tx tx = null;
		try {
			tx = TxManager.start();
			Object o = DaoManager.getDao().get(SoaDomain.class, id);
			DaoManager.getDao().delete(o);
			DaoManager.getDao().flush();
			tx.commit();			
		} catch (Throwable e) {
			logger.error("DomainDao.removeDomain(String)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"removeDomain(String) error",e);
		}finally{
			tx.end();
		}
		
	}
	
	
	public static SoaDomain getDomain(String id)throws BifangException{
		Tx tx = null;
		try {
			tx = TxManager.start();
			SoaDomain o = (SoaDomain)DaoManager.getDao().get(SoaDomain.class, id);
			
			tx.commit();	
			return o;
		} catch (Throwable e) {
			logger.error("DomainDao.getDomain(String)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"getDomain(String) error",e);
		}finally{
			tx.end();
		}
		
	}
	
	public static List getDomainByName(String name)throws BifangException{
		Tx tx = null;
		try {
			tx = TxManager.start();
			String hql = "from SoaDomain s where s.name = ?";
				
			List o = DaoManager.getDao().find(hql,name);
			
			tx.commit();	
			return o;
		} catch (Throwable e) {
			logger.error("DomainDao.getDomain(String)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,"getDomain(String) error",e);
		}finally{
			tx.end();
		}
		
	}

}
